import api from '../vuex/api';
import { Toast,Indicator } from 'mint-ui';

const appid = 'wx6a539a88d5c768d7';

export function share(title){
	const appid = "wx6a539a88d5c768d7";
    let _self = this;
    let params;
    params = [].splice.call(arguments, 0);
    var url = params[3] || location.href.split("#")[0];
    // api.get(`/v1/token/js_sign?appid=${appid}&url=${url}`).then(response => {
    //     wx.config(response.data.data);
    // });
      var title = title;
      var desc = "圈内大咖都在定制的医疗健康服务会员卡。";
      var imgUrl = params[2] || "https://imgs.2020-2020.com/MART/6088bf3969e951df/01f4d5ab3b756807.jpg";
      var link = params[3] || location.href.split("#")[0];
      var conf = {
        title: title, // 分享标题
        link: link, // 分享链接
        desc: desc,
        imgUrl: imgUrl, // 分享图标
        success: function(data) {
        },
        cancel: function(data) {
        }
      };
      wx.ready(function() {
        wx.onMenuShareTimeline(conf);
        wx.onMenuShareQZone(conf);
        wx.onMenuShareWeibo(conf);
        wx.onMenuShareQQ(conf);
        wx.onMenuShareAppMessage(conf);
      });
}


//初始化微信分享
export function wechatInit() {
		//title=null,desc=null,imgUrl=null,link=null,okCb=null,cancelCb=null
		var shareConf,option,params;
		params = [].splice.call(arguments,0);
		var url = params[3] || location.href.split('#')[0];
		api.get(`/v1/token/js_sign?appid=${appid}&url=${url}`)
		.then((response) => {
				response.data.data.jsApiList.push('openAddress');
				wechatForReady(response.data.data,params);
		});
}


function onBridgeReady(data,okCb,cancelCb) {
		WeixinJSBridge.invoke(
				'getBrandWCPayRequest', data,
				function(res) {
						switch (res.err_msg) {
								case "get_brand_wcpay_request:ok": //支付成功
										okCb && okCb();
										break;
								case "get_brand_wcpay_request:cancel": //取消支付
										cancelCb && cancelCb();
										break;
								default:
										alert(res.err_msg);
										break;
						}
				}
		);
}


export function wechatPay(url, conf, okCb, cancelCb){
	Indicator.open({
		text: 'Loading...'
	});
	api.post(url,conf)
  .then(function (response) {
			Indicator.close();
			var data = response.data;
			if(data.error == 0) {
			  if (typeof WeixinJSBridge == "undefined") {
					if (document.addEventListener) {
							document.addEventListener('WeixinJSBridgeReady', onBridgeReady, false);
					} else if (document.attachEvent) {
							document.attachEvent('WeixinJSBridgeReady', onBridgeReady);
							document.attachEvent('onWeixinJSBridgeReady', onBridgeReady);
					}
				} else {
					onBridgeReady(data.data,okCb,cancelCb);
				}
			} else {
				Toast(data.message);
			}
	 }).catch(function (error) {
    	console.log(error);
  });
}

export function wechatAuth(url,option){
	let params = option || { appid:appid,redirect_uri:location.href.split('#')[0],scope: 'userinfo'};
	api.post(url,params).then(response => {
		location.href=response.data.data;
	});
}


export function wechatLoginByCode(url,code){
	return api.get(`${url}?appid=${appid}&code=${code}`).then(response => {
			let res = response.data;
			if(res.error==0) {
				return res.data;
			} else {
				Toast(response.data.message);
			}
	});
}
